package testing;

import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import modelovectorial.DocumentoPDF;
import modelovectorial.core.SearchEngine;
import modelovectorial.core.SearchResult;
import modelovectorial.core.SuggestResult;
import soporte.MySQLConnexion;
import soporte.web.HighlighResultMatchs;

/**
 * @project DLC Final / UTN FRC 2013
 * @date Febrero de 2013
 *
 * @author Gonzalo Arribere
 * @author Gustavo Morales
 * @author Pablo Moreno
 */
public class Main {

    public static void parsearDirectorio() {
        System.out.println("Leyendo diccionario...");

        System.out.println(System.getProperty("user.dir"));
        File directorioPDF = new File("pdfs");

        File[] archivos = directorioPDF.listFiles();
        SearchEngine se = new SearchEngine();
        for (int i = 0; i < archivos.length; i++) {
            se.IndexFile("pdfs", archivos[i].getName());
        }
        try {
            MySQLConnexion.getConnexion().close();
        } catch (SQLException e) {
            System.out.println(e.getStackTrace());
        }
        System.exit(0);
    }

    public static void main(String[] args) throws IOException {
        MySQLConnexion.getConnexion();
        parsearDirectorio();
        sugerir();
    }

    public static void buscar() {

        long init = System.currentTimeMillis();

        SearchEngine engine = new SearchEngine();

        SearchResult result = engine.search("guerra ", 1);

        HighlighResultMatchs highlighResults = new HighlighResultMatchs(result.getProcessedQuery());

        double totalTime = (double) (System.currentTimeMillis() - init) / 1000D;

        for (int i = 0; i < result.results(); i++) {
            DocumentoPDF doc = result.getDocumentAt(i);
            System.out.println("--------------------------------------------------");
            System.out.println(String.format("idDoc: %d - w: %.10f", doc.getIdDocument(), doc.getTfidf()));
            System.out.println("--------------------------------------------------");

            System.out.println(String.format("idDoc: %d - fileName: %s - pages: %d - w: %.3f", doc.getIdDocument(),
                    doc.getRuta(),
                    doc.getPaginas(),
                    doc.getTfidf()));
            System.out.println("-- Fragment: " + doc.getDocumentFragment());
            System.out.println("-- Fragment (HTML): " + highlighResults.doIt(doc.getDocumentFragment()));
        }

        System.out.println(String.format("------------ Build Time : %.6f Second(s) --------------", totalTime));

        try {
            MySQLConnexion.getConnexion().close();
        } catch (Exception e) {
            System.out.println(e.getStackTrace());
        }
    }

    public static void sugerir() {

        long init = System.currentTimeMillis();

        SearchEngine engine = new SearchEngine();

        SuggestResult result = engine.suggest("de la ");

        double totalTime = (double) (System.currentTimeMillis() - init) / 1000D;

        for (int i = 0; i < result.results(); i++) {
            DocumentoPDF doc = result.getDocumentAt(i);
            System.out.println("--------------------------------------------------");
            System.out.println(String.format("idDoc: %d - w: %.10f", doc.getIdDocument(), doc.getTfidf()));
            System.out.println("--------------------------------------------------");

            System.out.println(String.format("idDoc: %d - fileName: %s - pages: %d - w: %.3f", doc.getIdDocument(),
                    doc.getRuta(),
                    doc.getPaginas(),
                    doc.getTfidf()));
            System.out.println("-- Fragment: " + doc.getDocumentFragment());
        }

        System.out.println(String.format("------------ Build Time : %.6f Second(s) --------------", totalTime));

        try {
            MySQLConnexion.getConnexion().close();
        } catch (Exception e) {
            System.out.println(e.getStackTrace());
        }
        System.exit(0);
    }
}